﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class DUDieuChuyenKhoChiTietBO : Csla.BusinessBase<DUDieuChuyenKhoChiTietBO>
    {
        #region Business Properties

        private int? _DUDieuChuyenKhoChiTietID = null;
        public int? DUDieuChuyenKhoChiTietID
        {
            get { return this._DUDieuChuyenKhoChiTietID; }
            set { this._DUDieuChuyenKhoChiTietID = value; PropertyHasChanged("DUDieuChuyenKhoChiTietID"); }
        }

        private int? _DUDieuChuyenKhoID = null;
        public int? DUDieuChuyenKhoID
        {
            get { return this._DUDieuChuyenKhoID; }
            set { this._DUDieuChuyenKhoID = value; PropertyHasChanged("DUDieuChuyenKhoID"); }
        }

        private int? _ThuocID = null;
        public int? ThuocID
        {
            get { return this._ThuocID; }
            set { this._ThuocID = value; PropertyHasChanged("ThuocID"); }
        }

        private int? _TonKhoIDN = null;
        public int? TonKhoIDN
        {
            get { return this._TonKhoIDN; }
            set { this._TonKhoIDN = value; PropertyHasChanged("TonKhoIDN"); }
        }

        private int? _DonViTinhIDN = null;
        public int? DonViTinhIDN
        {
            get { return this._DonViTinhIDN; }
            set { this._DonViTinhIDN = value; PropertyHasChanged("DonViTinhIDN"); }
        }

        private string _MaThuoc = null;
        public string MaThuoc
        {
            get { return this._MaThuoc; }
            set { this._MaThuoc = value; PropertyHasChanged("MaThuoc"); }
        }

        private int? _KhoIDN = null;
        public int? KhoIDN
        {
            get { return this._KhoIDN; }
            set { this._KhoIDN = value; PropertyHasChanged("KhoIDN"); }
        }

        private int? _TonKhoIDD = null;
        public int? TonKhoIDD
        {
            get { return this._TonKhoIDD; }
            set { this._TonKhoIDD = value; PropertyHasChanged("TonKhoIDD"); }
        }

        private int? _ThuocIDD = null;
        public int? ThuocIDD
        {
            get { return this._ThuocIDD; }
            set { this._ThuocIDD = value; PropertyHasChanged("ThuocIDD"); }
        }

        private int? _DonViTinhIDD = null;
        public int? DonViTinhIDD
        {
            get { return this._DonViTinhIDD; }
            set { this._DonViTinhIDD = value; PropertyHasChanged("DonViTinhIDD"); }
        }

        private double? _SoLuongChuyen = null;
        public double? SoLuongChuyen
        {
            get { return this._SoLuongChuyen; }
            set { this._SoLuongChuyen = value; PropertyHasChanged("SoLuongChuyen"); }
        }

        private double? _HeSoQuyDoi = null;
        public double? HeSoQuyDoi
        {
            get { return this._HeSoQuyDoi; }
            set { this._HeSoQuyDoi = value; PropertyHasChanged("HeSoQuyDoi"); }
        }

        private double? _SoLuongQuyDoi = null;
        public double? SoLuongQuyDoi
        {
            get { return this._SoLuongQuyDoi; }
            set { this._SoLuongQuyDoi = value; PropertyHasChanged("SoLuongQuyDoi"); }
        }

        private int? _KhoIDD = null;
        public int? KhoIDD
        {
            get { return this._KhoIDD; }
            set { this._KhoIDD = value; PropertyHasChanged("KhoIDD"); }
        }

        private DateTime? _NgayHetHan = null;
        public DateTime? NgayHetHan
        {
            get { return this._NgayHetHan; }
            set { this._NgayHetHan = value; PropertyHasChanged("NgayHetHan"); }
        }

        private string _DienGiai = null;
        public string DienGiai
        {
            get { return this._DienGiai; }
            set { this._DienGiai = value; PropertyHasChanged("DienGiai"); }
        }

        private int? _UserCreated = Session.LoginID;
        public int? UserCreated
        {
            get { return this._UserCreated; }
            set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
        }

        private DateTime? _DateCreated = DateTime.MaxValue;
        public DateTime? DateCreated
        {
            get { return this._DateCreated; }
            set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
        }

        private int? _UserModify = null;
        public int? UserModify
        {
            get { return this._UserModify; }
            set { this._UserModify = value; PropertyHasChanged("UserModify"); }
        }

        private DateTime? _DateModify = null;
        public DateTime? DateModify
        {
            get { return this._DateModify; }
            set { this._DateModify = value; PropertyHasChanged("DateModify"); }
        }

        private bool? _Deleted = false;
        public bool? Deleted
        {
            get { return this._Deleted; }
            set { this._Deleted = value; PropertyHasChanged("Deleted"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _DUDieuChuyenKhoChiTietID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newDUDieuChuyenKhoChiTietID = -1;
        private DUDieuChuyenKhoChiTietBO()
        {
            _DUDieuChuyenKhoChiTietID = newDUDieuChuyenKhoChiTietID--;
        }

        public DUDieuChuyenKhoChiTietBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public DUDieuChuyenKhoChiTietBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static DUDieuChuyenKhoChiTietBO NewRootDUDieuChuyenKhoChiTietBO()
        {
            return DataPortal.Create<DUDieuChuyenKhoChiTietBO>(new RootCriteria());
        }

        internal static DUDieuChuyenKhoChiTietBO NewChildDUDieuChuyenKhoChiTietBO()
        {
            return DataPortal.Create<DUDieuChuyenKhoChiTietBO>(new ChildCriteria());
        }

        public static DUDieuChuyenKhoChiTietBO GetRootDUDieuChuyenKhoChiTietBO(int _DUDieuChuyenKhoChiTietID)
        {
            return DataPortal.Fetch<DUDieuChuyenKhoChiTietBO>(new RootCriteria(_DUDieuChuyenKhoChiTietID));
        }

        public static DUDieuChuyenKhoChiTietBO GetRootDUDieuChuyenKhoChiTietBOByWhere(string where)
        {
            return DataPortal.Fetch<DUDieuChuyenKhoChiTietBO>(where);
        }

        internal static DUDieuChuyenKhoChiTietBO GetChildDUDieuChuyenKhoChiTietBO(SqlDataReader dataReader)
        {
            return new DUDieuChuyenKhoChiTietBO(dataReader);
        }

        public static void DeleteDUDieuChuyenKhoChiTietBO(int _DUDieuChuyenKhoChiTietID)
        {
            DataPortal.Delete(new RootCriteria(_DUDieuChuyenKhoChiTietID));
        }

        public static void DeleteDUDieuChuyenKhoChiTietBO(int _DUDieuChuyenKhoChiTietID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_DUDieuChuyenKhoChiTietID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int DUDieuChuyenKhoChiTietID;
            public bool directDelete = false;
            public RootCriteria(int _DUDieuChuyenKhoChiTietID)
            {
                this.DUDieuChuyenKhoChiTietID = _DUDieuChuyenKhoChiTietID;
                this.directDelete = false;
            }

            public RootCriteria(int _DUDieuChuyenKhoChiTietID, bool directDelete)
            {
                this.DUDieuChuyenKhoChiTietID = _DUDieuChuyenKhoChiTietID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int DUDieuChuyenKhoChiTietID;
            public bool directDelete = false;
            public ChildCriteria(int _DUDieuChuyenKhoChiTietID)
            {
                this.DUDieuChuyenKhoChiTietID = _DUDieuChuyenKhoChiTietID;
                this.directDelete = false;
            }

            public ChildCriteria(int _DUDieuChuyenKhoChiTietID, bool directDelete)
            {
                this.DUDieuChuyenKhoChiTietID = _DUDieuChuyenKhoChiTietID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUDieuChuyenKhoChiTietBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[DUDieuChuyenKhoChiTietID] = " + criteria.DUDieuChuyenKhoChiTietID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUDieuChuyenKhoChiTietBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["DUDieuChuyenKhoChiTietID"] is DBNull))
                this._DUDieuChuyenKhoChiTietID = (int)dataReader["DUDieuChuyenKhoChiTietID"];
            if (!(dataReader["DUDieuChuyenKhoID"] is DBNull))
                this._DUDieuChuyenKhoID = (int)dataReader["DUDieuChuyenKhoID"];
            if (!(dataReader["ThuocID"] is DBNull))
                this._ThuocID = (int)dataReader["ThuocID"];
            if (!(dataReader["TonKhoIDN"] is DBNull))
                this._TonKhoIDN = (int)dataReader["TonKhoIDN"];
            if (!(dataReader["DonViTinhIDN"] is DBNull))
                this._DonViTinhIDN = (int)dataReader["DonViTinhIDN"];
            if (!(dataReader["MaThuoc"] is DBNull))
                this._MaThuoc = (string)dataReader["MaThuoc"];
            if (!(dataReader["KhoIDN"] is DBNull))
                this._KhoIDN = (int)dataReader["KhoIDN"];
            if (!(dataReader["TonKhoIDD"] is DBNull))
                this._TonKhoIDD = (int)dataReader["TonKhoIDD"];
            if (!(dataReader["ThuocIDD"] is DBNull))
                this._ThuocIDD = (int)dataReader["ThuocIDD"];
            if (!(dataReader["DonViTinhIDD"] is DBNull))
                this._DonViTinhIDD = (int)dataReader["DonViTinhIDD"];
            if (!(dataReader["SoLuongChuyen"] is DBNull))
                this._SoLuongChuyen = (double)dataReader["SoLuongChuyen"];
            if (!(dataReader["HeSoQuyDoi"] is DBNull))
                this._HeSoQuyDoi = (double)dataReader["HeSoQuyDoi"];
            if (!(dataReader["SoLuongQuyDoi"] is DBNull))
                this._SoLuongQuyDoi = (double)dataReader["SoLuongQuyDoi"];
            if (!(dataReader["KhoIDD"] is DBNull))
                this._KhoIDD = (int)dataReader["KhoIDD"];
            if (!(dataReader["NgayHetHan"] is DBNull))
                this._NgayHetHan = (DateTime)dataReader["NgayHetHan"];
            if (!(dataReader["DienGiai"] is DBNull))
                this._DienGiai = (string)dataReader["DienGiai"];
            if (!(dataReader["UserCreated"] is DBNull))
                this._UserCreated = (int)dataReader["UserCreated"];
            if (!(dataReader["DateCreated"] is DBNull))
                this._DateCreated = (DateTime)dataReader["DateCreated"];
            if (!(dataReader["UserModify"] is DBNull))
                this._UserModify = (int)dataReader["UserModify"];
            if (!(dataReader["DateModify"] is DBNull))
                this._DateModify = (DateTime)dataReader["DateModify"];
            if (!(dataReader["Deleted"] is DBNull))
                this._Deleted = (bool)dataReader["Deleted"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._UserCreated = Session.LoginID;
            this._DateCreated = DateTime.MaxValue;
            this._UserModify = null;
            this._DateModify = null;
            this._Deleted = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUDieuChuyenKhoChiTietBO_InsertBO");
            db.AddInParameter(cmd, "@DUDieuChuyenKhoID", SqlDbType.Int, this._DUDieuChuyenKhoID);
            db.AddInParameter(cmd, "@ThuocID", SqlDbType.Int, this._ThuocID);
            db.AddInParameter(cmd, "@TonKhoIDN", SqlDbType.Int, this._TonKhoIDN);
            db.AddInParameter(cmd, "@DonViTinhIDN", SqlDbType.Int, this._DonViTinhIDN);
            db.AddInParameter(cmd, "@MaThuoc", SqlDbType.NVarChar, this._MaThuoc);
            db.AddInParameter(cmd, "@KhoIDN", SqlDbType.Int, this._KhoIDN);
            db.AddInParameter(cmd, "@TonKhoIDD", SqlDbType.Int, this._TonKhoIDD);
            db.AddInParameter(cmd, "@ThuocIDD", SqlDbType.Int, this._ThuocIDD);
            db.AddInParameter(cmd, "@DonViTinhIDD", SqlDbType.Int, this._DonViTinhIDD);
            db.AddInParameter(cmd, "@SoLuongChuyen", SqlDbType.Float, this._SoLuongChuyen);
            db.AddInParameter(cmd, "@HeSoQuyDoi", SqlDbType.Float, this._HeSoQuyDoi);
            db.AddInParameter(cmd, "@SoLuongQuyDoi", SqlDbType.Float, this._SoLuongQuyDoi);
            db.AddInParameter(cmd, "@KhoIDD", SqlDbType.Int, this._KhoIDD);
            db.AddInParameter(cmd, "@NgayHetHan", SqlDbType.DateTime, this._NgayHetHan);
            db.AddInParameter(cmd, "@DienGiai", SqlDbType.NVarChar, this._DienGiai);
            db.AddInParameter(cmd, "@UserCreated", SqlDbType.Int, this._UserCreated);
            db.AddOutParameter(cmd, "@NewDUDieuChuyenKhoChiTietID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDateCreated", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._DUDieuChuyenKhoChiTietID = (int)cmd.Parameters["@NewDUDieuChuyenKhoChiTietID"].Value;
            this._DateCreated = (DateTime)cmd.Parameters["@NewDateCreated"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._UserModify = Session.LoginID;
            this._DateModify = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUDieuChuyenKhoChiTietBO_UpdateBO");
            db.AddInParameter(cmd, "@DUDieuChuyenKhoChiTietID", SqlDbType.Int, this._DUDieuChuyenKhoChiTietID);
            db.AddInParameter(cmd, "@DUDieuChuyenKhoID", SqlDbType.Int, this._DUDieuChuyenKhoID);
            db.AddInParameter(cmd, "@ThuocID", SqlDbType.Int, this._ThuocID);
            db.AddInParameter(cmd, "@TonKhoIDN", SqlDbType.Int, this._TonKhoIDN);
            db.AddInParameter(cmd, "@DonViTinhIDN", SqlDbType.Int, this._DonViTinhIDN);
            db.AddInParameter(cmd, "@MaThuoc", SqlDbType.NVarChar, this._MaThuoc);
            db.AddInParameter(cmd, "@KhoIDN", SqlDbType.Int, this._KhoIDN);
            db.AddInParameter(cmd, "@TonKhoIDD", SqlDbType.Int, this._TonKhoIDD);
            db.AddInParameter(cmd, "@ThuocIDD", SqlDbType.Int, this._ThuocIDD);
            db.AddInParameter(cmd, "@DonViTinhIDD", SqlDbType.Int, this._DonViTinhIDD);
            db.AddInParameter(cmd, "@SoLuongChuyen", SqlDbType.Float, this._SoLuongChuyen);
            db.AddInParameter(cmd, "@HeSoQuyDoi", SqlDbType.Float, this._HeSoQuyDoi);
            db.AddInParameter(cmd, "@SoLuongQuyDoi", SqlDbType.Float, this._SoLuongQuyDoi);
            db.AddInParameter(cmd, "@KhoIDD", SqlDbType.Int, this._KhoIDD);
            db.AddInParameter(cmd, "@NgayHetHan", SqlDbType.DateTime, this._NgayHetHan);
            db.AddInParameter(cmd, "@DienGiai", SqlDbType.NVarChar, this._DienGiai);
            db.AddInParameter(cmd, "@UserModify", SqlDbType.Int, this._UserModify);
            db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this._Deleted);

            db.AddOutParameter(cmd, "@NewDateModify", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DateModify = (DateTime)cmd.Parameters["@NewDateModify"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_DUDieuChuyenKhoChiTietID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUDieuChuyenKhoChiTietBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@DUDieuChuyenKhoChiTietID", SqlDbType.Int, criteria.DUDieuChuyenKhoChiTietID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_DUDieuChuyenKhoChiTietID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
